package com.example.bookmanager;

import java.io.IOException;
import java.util.ArrayList;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.JavaType;

import android.app.Activity;
import android.app.AlertDialog;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.xuyang.bean.BorrowBook;
import com.xuyang.other.Flags;
import com.xuyang.other.ProgressDialog;

public class HistoryBorrowActivity extends Activity {
	private ImageView mImgFinish;
	private AlertDialog mDialog;
	private RequestQueue mQueue;
	private ListView mListView;
	private ArrayList<BorrowBook> list;
	private MyAdapter mAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_borrow_history);
		init();
		setOnClick();
		String url=Flags.BORROW_HISTORY.replace("UNAME", Flags.UserName).replace("FLAG", "2");
		StringRequest request=new StringRequest(url,new  Response.Listener<String>() {
			@Override
			public void onResponse(String str) {
				ObjectMapper mapper=new ObjectMapper();
				JavaType type=mapper.getTypeFactory().constructParametricType(ArrayList.class, BorrowBook.class);
					try {
						list=mapper.readValue(str, type);
					} catch (Exception e) {
						e.printStackTrace();
					} 
					mAdapter.notifyDataSetChanged();
					mDialog.dismiss();
			}
		}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError arg0) {
				Toast.makeText(getApplication(), "查询失败，请检查网络状态", 0).show();
				mDialog.dismiss();
			}
		});
		mQueue.add(request);
		mDialog.show();
	}
	private void init() {
		mImgFinish=(ImageView) findViewById(R.id.img_borrowhistory_finish);
		mDialog=new ProgressDialog(this).getDialog();
		mQueue=Volley.newRequestQueue(getApplicationContext());
		mListView=(ListView) findViewById(R.id.lv_borrowhistory);
		mAdapter=new MyAdapter();
		mListView.setAdapter(mAdapter);
	}
	
	private void setOnClick() {
		mImgFinish.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	class MyAdapter extends BaseAdapter{
		@Override
		public int getCount() {
			if (list==null) {
				return 0;
			}
			return list.size();
		}
		@Override
		public Object getItem(int position) {
			return list.get(position);
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView==null) {
				convertView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_borrow, parent,false);
			}
			TextView mTxtName=(TextView) convertView.findViewById(R.id.txt_borrow_bookname);
			TextView mTxtTime=(TextView) convertView.findViewById(R.id.txt_borrow_time);
			mTxtName.setText(list.get(position).getBookName());
			mTxtTime.setText(list.get(position).getBorrowTime());
			return convertView;
		}
		
	}
}
